package com.sw.youvote.client.quizwizard;

import java.util.ArrayList;
import java.util.List;


import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sw.youvote.client.QuizListGUI;
import com.sw.youvote.shared.objects.CandidatureBean;
import com.sw.youvote.shared.objects.QuizBean;

public class FormularioCreacionArea {
	
    private final int NUEVO = 0;
	private final int ACTUALIZADO = 1;
	private final int CREADO = 2;

	private TextBox nameField;
	private TextBox descriptionField;
	private TextBox creatormailField;
	private TextBox imageurlField;
	private VerticalPanel candidatureList;
	private Grid formGrid;
	private Button addButton;
	private Button updateButton;
	private Button addCandidatureButton;
	private PopupPanel panel;
	private QuizBean actualQuiz;
	private QuizListGUI gui;
	private FormularioUploadImagen formularioUpload;
	private int estado;
	
	private AddCandidatureForm formCandidature;
	
	
	
	public FormularioCreacionArea(QuizListGUI parentGui){
		
		/*formulario */
        nameField = new TextBox();
        descriptionField = new TextBox();
        creatormailField = new TextBox();
        imageurlField = new TextBox();
        candidatureList = new VerticalPanel();
        formularioUpload = new FormularioUploadImagen();
        
        formGrid = new Grid(7,3);
        formGrid.setVisible(false);
        //formGrid.setBorderWidth(1);
     
    	// WIDGET - boton add 
      	addButton = new Button("Add new Quiz");
      	// WIDGET - boton update
      	updateButton = new Button("Update Quiz");
      	// WIDGET - boton anadir candidatura
      	addCandidatureButton = new Button("Add new Candidature");
      	
      	// WIDGET - boton para upload

        
        formGrid.setWidget(0, 0, new Label("Name"));
        formGrid.setWidget(0, 1, nameField);

        formGrid.setWidget(1, 0, new Label("Description"));
        formGrid.setWidget(1, 1, descriptionField);
        
        formGrid.setWidget(2, 0, new Label("Owner email"));
        formGrid.setWidget(2, 1, creatormailField);
        
        formGrid.setWidget(3, 0, new Label("Image URL"));
        formGrid.setWidget(3, 1, imageurlField);
        
        formGrid.setWidget(4, 0, new Label("Candidaturas"));
        formGrid.setWidget(4, 1, candidatureList);
        
        formGrid.setWidget(5, 0, updateButton);
        formGrid.setWidget(5, 1, addButton);
        
        formGrid.setWidget(6, 0, addCandidatureButton);
        formGrid.setWidget(6, 1, formularioUpload.getFileUploaderWidget());
        
        panel = new PopupPanel();
        
        panel.setPopupPosition(
        		RootPanel.get().getOffsetWidth()/2 - panel.getOffsetWidth()/2, 
        		RootPanel.get().getOffsetHeight()/2);
    	panel.setWidget(formGrid);
    	gui = parentGui;
    	formCandidature = new AddCandidatureForm(gui);
    	
	}
	
	/**
	 * 
	 * @param quiz
	 * @param operation
	 *           1- Creando nuevo concurso
	 *           2- Actualizando concurso
	 */
	public void loadForm(QuizBean quiz, int operation) {
		
		
    	this.panel.show();
    	this.actualQuiz = quiz;
    	this.formGrid.setVisible(true);
    	
        this.descriptionField.setText(quiz.getDescription());
        this.creatormailField.setText(quiz.getCreatorMail());
        this.nameField.setText(quiz.getName());   
        this.imageurlField.setText(quiz.getImageurl());   
        
        // Creando concurso nuevo. Limpiar candidaturas
        if (operation == 1) {
        	formGrid.setWidget(4, 1, new VerticalPanel());
        }
        // Actualizando Concurso. Mostrando candidaturas asociadas
        if (operation == 2) {
        	gui.getQuizService().listCandidatures(quiz);
        }
        //this.imageurlField.setText(quiz.getCandidatureSets()!=null?quiz.getCandidatureSets().toString():"null");
        
        //updateCandidatures(quiz.getCandidatureSets());
        //  this.imageurlField.setText(quiz.getCandidatureSets().toString()) ;     
     }
	
	public void listCandidatures(List<CandidatureBean> result) {
		this.candidatureList.setVisible(false);
		this.candidatureList = null;
		VerticalPanel temp = new VerticalPanel();
		if (result!= null) {
        	        	
        		//for (int i = 0; i < result.size(); i++) {
        		//	candidature = result.get(i);
        		//	if (candidature != null) {
        		//		this.candidatureList.add(new RadioButton("candidature", candidature.getName()));
        		//	}
        		//}
			
				for (CandidatureBean candidature: result) {
					if (candidature != null) {
		        		temp.add(new RadioButton("candidature", candidature.getName()));
					}
				}
        	
        	
        }
		
		this.candidatureList = temp;
		formGrid.setWidget(4, 1, this.candidatureList);
		this.candidatureList.setVisible(true);
		
		
		
	}
	public void gui_eventAddButtonClicked() {
        gui.getSidebarArea().getAddNewButton().setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToContact();
        descriptionField.getText();
        gui.getQuizService().addQuiz(this.actualQuiz);
        panel.hide();
    }

    public void gui_eventUpdateButtonClicked() {
    	gui.getSidebarArea().getAddNewButton().setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToContact();
        gui.getQuizService().updateQuiz(this.actualQuiz);
        panel.hide();
    }
    
    // Evento lanzado cuando se pulsa boton de añadir nueva candidatura
    public void gui_eventAddCandidatureButtonClicked() {
    	
    	formCandidature.loadForm(this.actualQuiz);
    	
    }

    private void copyFieldDateToContact() {
    	if (this.actualQuiz== null) 
    		this.actualQuiz = new QuizBean();
    	
    	this.actualQuiz.setDescription(descriptionField.getText());
    	this.actualQuiz.setName(nameField.getText());
    	this.actualQuiz.setCreatorMail(creatormailField.getText());
    	this.actualQuiz.setImageurl(imageurlField.getText());
    	//gui.getCurrentQuiz().setCandidatureSets(new ArrayList());
    	
    	
    }
    
     private void controlBPM(){
    	if (estado == NUEVO){
    		panel.hide();
    	}
    	if (estado == ACTUALIZADO){
    		panel.hide();
    	}
    	if (estado == CREADO){
    		panel.hide();
    	}
    	
    }
    
    public PopupPanel getPanel(){
		return panel;
	}
	
	public Button getUpdateButton(){
		return updateButton;
	}
	public Button getAddButton(){
		return addButton;
	}
	
	public Button getAddCandidatureButton(){
		return addCandidatureButton;
	}
	
	public AddCandidatureForm getAddCandidatureForm() {
		return formCandidature;
	}
	
}
